import mongoose, { Document, Schema } from 'mongoose'
import { NotificationType } from '../types/notification'

export interface INotification extends Document {
  userId: string
  type: NotificationType
  title: string
  content: string
  data?: any
  read: boolean
  createdAt: Date
}

const NotificationSchema = new Schema({
  userId: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
    index: true,
  },
  type: {
    type: String,
    required: true,
    enum: Object.values(NotificationType),
  },
  title: {
    type: String,
    required: true,
  },
  content: {
    type: String,
    required: true,
  },
  data: Schema.Types.Mixed,
  read: {
    type: Boolean,
    default: false,
  },
  createdAt: {
    type: Date,
    default: Date.now,
    index: true,
  },
})

// 索引
NotificationSchema.index({ userId: 1, createdAt: -1 })
NotificationSchema.index({ userId: 1, read: 1 })

export const Notification = mongoose.model<INotification>('Notification', NotificationSchema)
